package com.moleq.dialog;

import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.models.ItemDiscountSetting;
import com.moleq.mgpos.activities.R;

public class FollowItemDialog extends MoleQBaseDialog {

	private OnResultListener mOnResultListener;
	private CheckBox cb_follow_item,cb_tax1,cb_tax2,cb_tax3,cb_tax4,cb_tax5,cb_tax6;
	private Button btn_follow_item_ok,btn_follow_item_cancel;
	private Context mContext;
	private List<ItemDiscountSetting> listDiscount;
	private ItemDiscountSetting mItemDiscountSetting;
	private MessageDialog mMessageDialog;
	private int selectedRow = 0;
	
	public void onCloseWithReturn(Object obj){
		mOnResultListener.onReturnValue(0,obj);
		dismiss();
	}
	
	public FollowItemDialog(Context context) {
		super(context,context);
		constructor(context);
		
	}
	public FollowItemDialog(Context context, Object base) {
		super(context,base);
		constructor(context,base);
	}
	
	public void setSource(List<ItemDiscountSetting> listDiscount,ItemDiscountSetting itemDiscount,int row){
		this.listDiscount = listDiscount;
		this.mItemDiscountSetting = itemDiscount;
		this.selectedRow = row;
	}
	private void constructor(Context context,Object base) {
		mContext = context;
		mOnResultListener = (OnResultListener) base;
	}

	public FollowItemDialog(Context context, int theme) {
		super(context, theme);
		constructor(context);
	}
	
	private void constructor(Context context) {
		// TODO Auto-generated method stub
		mContext = context;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.ds_dialog_follow_item);
		this.setCancelable(false);
	
		cb_follow_item = (CheckBox) findViewById(R.id.cb_follow_item);
		cb_tax1 = (CheckBox) findViewById(R.id.cb_tax1);
		cb_tax2 = (CheckBox) findViewById(R.id.cb_tax2);
		cb_tax3 = (CheckBox) findViewById(R.id.cb_tax3);
		cb_tax4 = (CheckBox) findViewById(R.id.cb_tax4);
		cb_tax5 = (CheckBox) findViewById(R.id.cb_tax5);
		cb_tax6 = (CheckBox) findViewById(R.id.cb_tax6);
//		cb_fs = (CheckBox) findViewById(R.id.cb_fs);
		btn_follow_item_ok = (Button) findViewById(R.id.btn_follow_item_ok);
		btn_follow_item_cancel = (Button) findViewById(R.id.btn_follow_item_cancel);
		btn_follow_item_ok.setOnClickListener(OnClickDialogWindowListener);
		btn_follow_item_cancel.setOnClickListener(OnClickDialogWindowListener);
		cb_follow_item.setOnCheckedChangeListener(onCheckedChangeListener);
	}
	
	public void show(){
		super.show();	
		GetDetail(mItemDiscountSetting);
	}
	
	public Button.OnClickListener OnClickDialogWindowListener = new Button.OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_follow_item_ok:
				if (cb_follow_item.isChecked()) {
					mItemDiscountSetting.setDs_follow_item(true);
					mItemDiscountSetting.setDs_tax1(false);
					mItemDiscountSetting.setDs_tax2(false);
					mItemDiscountSetting.setDs_tax3(false);
					mItemDiscountSetting.setDs_tax4(false);
					mItemDiscountSetting.setDs_tax5(false);
					mItemDiscountSetting.setDs_tax6(false);
					mItemDiscountSetting.setDs_foodstamp(false);
				}else {
					mItemDiscountSetting.setDs_follow_item(cb_follow_item.isChecked()?true:false);
					mItemDiscountSetting.setDs_tax1(cb_tax1.isChecked()?true:false);
					mItemDiscountSetting.setDs_tax2(cb_tax2.isChecked()?true:false);
					mItemDiscountSetting.setDs_tax3(cb_tax3.isChecked()?true:false);
					mItemDiscountSetting.setDs_tax4(cb_tax4.isChecked()?true:false);
					mItemDiscountSetting.setDs_tax5(cb_tax5.isChecked()?true:false);
					mItemDiscountSetting.setDs_tax6(cb_tax6.isChecked()?true:false);
//					mItemDiscountSetting.setDs_foodstamp(cb_fs.isChecked()?true:false);
					mItemDiscountSetting.setDs_foodstamp(false);
				}
				listDiscount.set(selectedRow, mItemDiscountSetting);
				listDiscount.get(selectedRow).setEdit(true);
				onCloseWithReturn(listDiscount);
				dismiss();
				break;
			case R.id.btn_follow_item_cancel:
				dismiss();
				break;
			}
		}
	};
	
	OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if (cb_follow_item.isChecked()) {
				EnableControl(false);
			}else{
				EnableControl(true);
			}
		}
	};
	
	public void GetDetail(ItemDiscountSetting itemDiscount){
		cb_follow_item.setChecked(itemDiscount.isDs_follow_item());
		if (cb_follow_item.isChecked()) {
			EnableControl(false);
		}else {
			cb_tax1.setChecked(itemDiscount.isDs_tax1());
			cb_tax2.setChecked(itemDiscount.isDs_tax2());
			cb_tax3.setChecked(itemDiscount.isDs_tax3());
			cb_tax4.setChecked(itemDiscount.isDs_tax4());
			cb_tax5.setChecked(itemDiscount.isDs_tax5());
			cb_tax6.setChecked(itemDiscount.isDs_tax6());
//			cb_fs.setChecked(itemDiscount.isDs_foodstamp());
		}
	}
	
	public void EnableControl(boolean enable){
		
		if (!enable) {
			cb_tax1.setChecked(enable); 
			cb_tax2.setChecked(enable); 
			cb_tax3.setChecked(enable); 
			cb_tax4.setChecked(enable); 
			cb_tax5.setChecked(enable); 
			cb_tax6.setChecked(enable); 
//			cb_fs.setChecked(enable); 
		}else{
			
		}
		cb_tax1.setEnabled(enable); 
		cb_tax2.setEnabled(enable); 
		cb_tax3.setEnabled(enable); 
		cb_tax4.setEnabled(enable);  
		cb_tax5.setEnabled(enable);  
		cb_tax6.setEnabled(enable); 
//		cb_fs.setEnabled(enable); 
	}
	
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener(){

		public void onClickYes() {
			
		}

		public void onClickNo() {
			mMessageDialog.dismiss();
		}

		public void onClickOK() {
			mMessageDialog.dismiss();
		}
	};
	
	public void ShowMessage(String text,int type,int icon){
		mMessageDialog = new MessageDialog(mContext);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}
}
